<?php
include_once('services.common.php');

interface iHandler {
    public function getNodes($id_node,$id_user);
}

class DefaultHandler implements iHandler {
	
	public function getNodes($id_node,$id_user){
		$db = DataService::getInstance();
		$menu = array();
		$nodes = $db->getMenuOptions($id_node,$id_user);
		
		foreach($nodes as $node){
			$isLeaf = isset($node['id_parent']);
			array_push($menu, array('id'=> $node['id_menu'], 'text'=>$node['titulo'],'leaf'=>$isLeaf, 'url' => $node['url']));
		}
		return($menu);
	}
}

class System_Menu {

	  private function getHandlerInstance($class_name){
		$class = new ReflectionClass($class_name);
		return($class->newInstance());
	  }	
		/**
		* @remotable
	         */
	  public function get_menu_tree($id_nodo){
		$db = DataService::getInstance();
		$nodo = $db->getMenuOption($id_nodo);
		$handler = $this->getHandlerInstance($nodo['handler_class']);
		return($handler->getNodes($id_nodo,$_SESSION['id_usuario']));

	  }  
}
?>